package com.szgr.baoyin.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.ToastUtils;
import com.szgr.baoyin.utils.ViewFindUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 *评价服务人员
 */
public class EvaluateActivity extends BaseActivity {

    private String maintenance_id;
    private String score;
    private RatingBar rb_taidu;
    private RatingBar rb_sudu;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 20:
                    ToastUtils.showToast("评价成功");
                    finish();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_evaluate);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("服务评价");
    }

    @Override
    public void initView() {
        RadioButton rb_good = (RadioButton) findViewById(R.id.evaluate_good);
        RadioButton rb_center = (RadioButton) findViewById(R.id.evaluate_center);
        RadioButton rb_bad = (RadioButton) findViewById(R.id.evaluate_bad);
        EditText et_content = (EditText) findViewById(R.id.evaluate_content);
        rb_taidu = (RatingBar) findViewById(R.id.evaluate_taidu);
        rb_sudu = (RatingBar) findViewById(R.id.evaluate_sudu);
        Button btn_submit = (Button) findViewById(R.id.evaluate_submit);
        TextView tv_num = ViewFindUtils.find(view,R.id.eva_num);
        TextView tv_name = ViewFindUtils.find(view,R.id.eva_name);

        btn_submit.setOnClickListener(this);
        rb_good.setOnClickListener(this);
        rb_center.setOnClickListener(this);
        rb_bad.setOnClickListener(this);

        Intent intent = getIntent();
        if(intent != null && intent.hasExtra("maintenance_id")){
            maintenance_id = intent.getStringExtra("maintenance_id");
            String id = intent.getStringExtra("num");
            String name = intent.getStringExtra("name");
            tv_num.setText(id);
            tv_name.setText(name);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.evaluate_submit:
                doEvaluate();
                break;
            case R.id.evaluate_good:
                score = "01";
                break;
            case R.id.evaluate_center:
                score = "02";
                break;
            case R.id.evaluate_bad:
                score = "03";
                break;
        }
    }

    private void doEvaluate() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_EVALUATE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("---evaluate--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            boolean update_maintenance = datas.getBoolean("$update_maintenance");
                            if(update_maintenance){
                                mHandler.obtainMessage(20).sendToTarget();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("maintenance_id",maintenance_id);
                map.put("score",score);
                map.put("service_attitude",rb_taidu.getNumStars() + "");
                map.put("visit_speed",rb_sudu.getNumStars() + "");
                map.put("key",MyApplication.getmInstance().getKey());
                System.out.println("---params--->" + map.toString());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }
}
